package com.example.yx1.demo34;

//import org.springframework.boot.autoconfigure.web.servlet.WelcomePageHandlerMapping;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter;
import org.springframework.web.servlet.resource.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Configuration
public class WebConfig {
    @Bean
    public TomcatServletWebServerFactory servletWebServerFactory() {return new TomcatServletWebServerFactory();}

    @Bean
    public DispatcherServlet dispatcherServlet() {return new DispatcherServlet();}

    @Bean
    public DispatcherServletRegistrationBean servletRegistrationBean(DispatcherServlet dispatcherServlet) {
        return new DispatcherServletRegistrationBean(dispatcherServlet,"/");
    }

    //其他映射器在初始化时会自动收集handler，这个不会，因此需要手动收集一下
    @Bean
    public SimpleUrlHandlerMapping simpleUrlHandlerMapping(ApplicationContext applicationContext) {
        Map<String, ResourceHttpRequestHandler> map = applicationContext.getBeansOfType(ResourceHttpRequestHandler.class);
        SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
        handlerMapping.setUrlMap(map);
        System.out.println(map);
        return handlerMapping;
    }

    @Bean
    public HttpRequestHandlerAdapter httpRequestHandlerAdapter() {
        return new HttpRequestHandlerAdapter();
    }

    //任意路径均可去访问static下的资源
    @Bean("/**")
    public ResourceHttpRequestHandler handler1() {
        ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
        List<Resource> list = new ArrayList<>();
        list.add(new ClassPathResource("static/"));
        handler.setLocations(list);
        List<ResourceResolver> list1 = new ArrayList<>();
        list1.add(new CachingResourceResolver(new ConcurrentMapCache("cache1")));//读取资源可以加入缓存中
        list1.add(new EncodedResourceResolver());//读取压缩资源
        list1.add(new PathResourceResolver());//基础的根据路径访问
        handler.setResourceResolvers(list1);
        return handler;
    }

    //要求路径必须以/img/开头，这样才允许去访问images下的资源
    @Bean("/img/**")
    public ResourceHttpRequestHandler handler2() {
        ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
        List<Resource> list = new ArrayList<>();
        list.add(new ClassPathResource("images/"));
        handler.setLocations(list);
        return handler;
    }

//    @Bean
//    public WelcomePageHandlerMapping welcomePageHandlerMapping() {
//        return new WelcomePageHandlerMapping();
//    }
}
